const beaeUrl = 'https://xf.westts.cn/'

function request(method, url, data) {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '加载中'
		});
		wx.request({
			url: beaeUrl + url,
			data,
			header: {
				Authorization: uni.getStorageSync('token') || ''
			},
			method,
			success(res) {
				uni.hideLoading();
				switch (res.data.code) {
					case 401:
						uni.showToast({
							title: '登录信息过期',
							icon: "error"
						})
						setTimeout(() => {
							uni.redirectTo({
								url: '../login/login'
							})
						}, 1500);
						break;
					case 200:
						resolve(res.data)
						break;
				}

			},
			fail(err) {
				uni.hideLoading();
				wx.showModal({
					title: '提示',
					content: '网络断开，稍后重试！'
				})
				resolve(false)
			}
		})
	})
}

module.exports = {
	"get": function(url, data) {
		return request("GET", url, data);
	},
	"post": function(url, data) {
		return request("POST", url, data);
	}
	/*  "put": function(url, data, header) {
	      return request("PUT", url, data, header);
	  },
	  "delete": function(url, data, header) {
	      return request("DELETE", url, data, header);
	  } */
}
